package com.fh.fruit.service.pic.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.fh.dao.DaoSupport;
import com.fh.fruit.service.goods.GoodsManager;
import com.fh.fruit.service.pic.PictureManager;
import com.fh.fruit.util.FtpUtil;
import com.fh.fruit.util.IDUtils;


/**
 * 图片上传服务
 * 
 * @author Administrator
 *
 */
@Service
public class PictureSevice implements PictureManager {

	@Value("${FTP_ADDRESS}")
	private String FTP_ADDRESS;
	@Value("${FTP_PORT}")
	private Integer FTP_PORT;
	@Value("${FTP_USERNAME}")
	private String FTP_USERNAME;
	@Value("${FTP_PASSWORD}")
	private String FTP_PASSWORD;
	@Value("${FTP_IMAGE_BASE_PATH}")
	private String FTP_IMAGE_BASE_PATH;
	@Value("${IMAGE_BASE_URL}")
	private String IMAGE_BASE_URL;
	
	@Resource(name="goodsService")
	private GoodsManager goodsService;
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	@Override
	public Map<String, Object> uploadPicture(MultipartFile uploadFile) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 生成一个新的文件名
			// 取原始文件名
			String oldName = uploadFile.getOriginalFilename();
			// 生成新文件名
			// UUID.randomUUID();
			String newName = IDUtils.genImageName();
			newName = newName + oldName.substring(oldName.lastIndexOf("."));
			// 图片上传
			String imagePath = new DateTime().toString("/yyyy/MM/dd");
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_IMAGE_BASE_PATH,
					imagePath, newName, uploadFile.getInputStream());
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL +imagePath + "/" + newName);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}

	@Override
	public Map<String, Object> uploadPicture(MultipartFile uploadFile, String end, int width, int height, float quality) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 生成新文件名
			// UUID.randomUUID();
			String newName = IDUtils.genImageName();
			newName = newName + "_"+end+".jpg";
			// 图片上传
			String imagePath = new DateTime().toString("/yyyy/MM/dd");
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_IMAGE_BASE_PATH,
					imagePath, newName, uploadFile.getInputStream(),width, height, quality);
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL +imagePath + "/" + newName);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}

	
	
	@Override
	public Map<String, Object> uploadPicture(MultipartFile uploadFile, String imagePath, String newName, String end, int width,
			int height, float quality) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 生成新文件名
			// UUID.randomUUID();
			newName = newName + "_"+end+".jpg";
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_IMAGE_BASE_PATH,
					imagePath, newName, uploadFile.getInputStream(),width, height, quality);
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL +imagePath + "/" + newName);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}

	@Override
	public Map<String, Object> uploadPicture(MultipartFile uploadFile, String imagePath,String type) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {

			String newName =type;
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_IMAGE_BASE_PATH,
					imagePath, newName, uploadFile.getInputStream());
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", IMAGE_BASE_URL +imagePath + "/" + newName);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}
	
	@Override
	public Map<String, Object> imageDelete(Map<Object,String> param) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if(param!=null){
				goodsService.deleteImage(param);
				String imgPath=param.get("imgsrc").replace(IMAGE_BASE_URL,FTP_IMAGE_BASE_PATH);
				boolean result = FtpUtil.deleteFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD,imgPath);
				// 返回结果
				if (!result) {
					resultMap.put("error", 1);
					resultMap.put("message", "文件删除失败");
					return resultMap;
				}
				resultMap.put("error", 0);
				resultMap.put("message", "文件删除成功");
				return resultMap;
			}else{
				resultMap.put("error", 1);
				resultMap.put("message", "文件删除失败");
				return resultMap;
			}
		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件删除发生异常");
			e.printStackTrace();
			return resultMap;
		}
	}

}
